//
//  CedarAlertView.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/10/10.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "CedarAlertView.h"
#include <UIView+TYAlertView.h>

@interface CedarAlertView()<UITextViewDelegate>

@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *content;
@property(nonatomic,strong) NSArray<CedarAlertButtonStyle *> *buttonStyleArray;
@property(nonatomic,strong) UITextView *textView;

@end



@implementation CedarAlertView


- (instancetype)initWithTitle:(NSString *)title
                      content:(NSString *)content
                  buttonStyle:(CedarAlertButtonStyle *)style{
    return [self initWithTitle:title content:content lineHeadIndent:0 buttonStyle:style];
}

- (instancetype)initWithTitle:(NSString *)title
                      content:(NSString *)content
          lineHeadIndent:(CGFloat)value
                  buttonStyle:(CedarAlertButtonStyle *)style{
    if (self = [super init]) {
        self.backgroundColor=[UIColor whiteColor];
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        self.title = title;
        self.content = content;
        CGFloat scrollViewHeight = [self setViewFrame:value];
        self.buttonStyleArray = [NSArray arrayWithObject:style];
        [self setupSubviews:scrollViewHeight];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title content:(NSString *)content  buttonStyleArray:(NSArray<CedarAlertButtonStyle *> *)style{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        self.title = title;
        self.content = content;
        CGFloat scrollViewHeight = [self setViewFrame:0];
        self.buttonStyleArray = [NSArray arrayWithArray:style];
        [self setupSubviews:scrollViewHeight];
    }
    return self;
}

- (void)dealloc{
    LogDealloc
}

- (CGFloat)setViewFrame:(CGFloat)value{
    CGFloat height = 0;
    CGFloat maxSize = SCREEN_WIDTH -  60 - 30;
    CGFloat surplusHeight = 120.0;
    CGFloat fixedHeight = SCREEN_HEIGHT-200;//view 最大显示的高度
    CGFloat scrollMaxHeight = fixedHeight- surplusHeight;//文本最大显示高度
    UIFont *font = PingFangZhongChangGuiForFont(15);
    CGFloat lineFragmentPadding = [self.textView.textContainer lineFragmentPadding] * 2; //10
    CGSize size;
    CGSize textViewShowSize = CGSizeMake(maxSize-lineFragmentPadding, fixedHeight);
    if(value>0){//设置带有属性的文本
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.firstLineHeadIndent = value;
        NSDictionary *attributes = @{NSFontAttributeName:PingFangZhongChangGuiForFont(15),NSParagraphStyleAttributeName:paragraphStyle};
       size = [self.content boundingRectWithSize:textViewShowSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        self.textView.attributedText = [[NSAttributedString alloc] initWithString:self.content attributes:attributes];
    }else{
       size = [PlatformTools sizeWithString:self.content andFount:font andMaxSize:textViewShowSize];
    }
    CGFloat textViewHeight = 0;
     if (size.height>scrollMaxHeight) {
        size.height = scrollMaxHeight;
        textViewHeight = scrollMaxHeight;
    }
    height = size.height + surplusHeight;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH - 60, height);
    return textViewHeight;
}

- (void)setupSubviews:(CGFloat)scrollTextMaxHeight{
    UILabel *titleLbl = [[UILabel alloc]init];
    MACRO_UILabel_ConfigureNoFontSize(titleLbl, self.title, kBlackColor);
    titleLbl.numberOfLines = 0;
    titleLbl.textAlignment = NSTextAlignmentCenter;
    titleLbl.font = PingFangZhongChangGui_MediumForFont(18);
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(15);
    }];

    self.textView.scrollEnabled = scrollTextMaxHeight>0?YES:NO;
    self.textView.text =  self.content;
    [self addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.top.mas_equalTo(titleLbl.mas_bottom).offset(5);
        if(scrollTextMaxHeight>0){
            make.height.mas_offset(scrollTextMaxHeight);
        }
    }];
    if (IsEmptyStr(self.title)) {
        CGRect frame = self.frame;
        frame.size.height -= 18;
        self.frame = frame;
    }
    if( [self.buttonStyleArray count] == 1){//
        CedarAlertButtonStyle *style = self.buttonStyleArray[0];
        CedarButton *updateBtn =  [CedarButton buttonWithRegisterText:style.title];
        [updateBtn PFCGStyleFontSize:18.0];
        @weakify(self)
        updateBtn.clickEvent = ^{
            @strongify(self)
            [self hideView];
            if (style.clickBlock) {
                style.clickBlock();
            }
        };
        [self addSubview:updateBtn];
        [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.bottom.mas_offset(-18);
            make.height.mas_offset(38);
            make.width.mas_offset(240);
        }];
    }else  if( [self.buttonStyleArray count] == 2){
        UIView *hLineView =  [[UIView alloc]init];
        hLineView.backgroundColor = kSeparateLineColor;
        [self addSubview:hLineView];
        [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            //make.top.mas_equalTo(textView.mas_bottom).mas_offset(10);
            make.bottom.mas_equalTo(self).mas_offset(-46);
            make.height.mas_offset(0.5);
        }];
        
        CedarAlertButtonStyle *leftStyle = self.buttonStyleArray[0];
        CedarAlertButtonStyle *rightStyle = self.buttonStyleArray[1];
        UIView *vLineView =  [[UIView alloc]init];
        vLineView.backgroundColor = kSeparateLineColor;
        [self addSubview:vLineView];
        [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(hLineView).mas_offset(10);
            make.bottom.mas_equalTo(self).mas_offset(-10);
            make.width.mas_offset(0.5);
        }];
        
        CedarButton *leftButton =  [CedarButton buttonWithTextNoStytle:leftStyle.title];
        @weakify(self)
        leftButton.clickEvent = ^{
            @strongify(self)
            [self hideView];
            if (leftStyle.clickBlock) {
                leftStyle.clickBlock();
            }
        };
        [leftButton PFCGStyleTextColor:kGrayColor fontSize:18.0];
        [self addSubview:leftButton];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(vLineView);
            make.centerY.mas_equalTo(vLineView);
        }];
    
        CedarButton *rightButton =  [CedarButton buttonWithTextNoStytle:rightStyle.title];
        [rightButton setTitleColor:kMasterHighlightColor forState:UIControlStateHighlighted];
        rightButton.clickEvent = ^{
            @strongify(self)
            [self hideView];
            if (rightStyle.clickBlock) {
                rightStyle.clickBlock();
            }
        };
        [rightButton PFCGStyleTextColor:kMasterColor fontSize:18.0];
        [self addSubview:rightButton];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(vLineView);
            make.right.mas_equalTo(self);
            make.centerY.mas_equalTo(leftButton);
        }];
    }
}

- (void)configueUITextViewAttributes:(void (^)(UITextView *textView))textViewBlock{
    if (textViewBlock) {
        textViewBlock(self.textView);
    }
}

- (void)hideAlertView{
    [self hideView];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return NO;
}

- (UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]init];
        _textView.delegate = self;
        _textView.font = PingFangZhongChangGuiForFont(15);
        _textView.textColor = kBlackColor;
    }
    return _textView;
}

@end
@implementation CedarAlertButtonStyle

@end
